package com.example.administrator.myapplication.body;

import android.app.Dialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Toast;

import com.example.administrator.myapplication.BmobApplication;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.evaluate.TestAssess;


public class body_main_Fragment extends Fragment {
    private Toolbar toolbar;
    private Button bt_height;
    private Button bt_weight;
    private Button bt_body_submit;
    private LayoutInflater factory;
    private View myView;
    private int body_height = 0;
    private int body_weight = 0;
    private int numSaver;
    private int flag;              //判断是点击哪一个按钮
    private boolean  swich ;        //判断有没有选取;
    private BmobApplication body_apps;
    private NumberPicker numberpicker;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_body_main_, container, false);
        toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar_body);
        toolbar.setNavigationIcon(R.mipmap.custom_back);
        toolbar.setTitle("身体成分");
        toolbar.setTitleMarginStart(100);
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();

            }
        });

        bt_height = (Button) view.findViewById(R.id.bt_body_height);
        bt_weight = (Button) view.findViewById(R.id.bt_body_weight);
        bt_body_submit = (Button) view.findViewById(R.id.bt_body_submit);
        body_apps= (BmobApplication) getActivity().getApplication();


        bt_height.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetNumberpicker(R.id.bt_body_height);
                numberpicker.setValue(170);
            }
        });

        bt_weight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SetNumberpicker(R.id.bt_body_weight);
                numberpicker.setValue(50);
            }
        });


        bt_body_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(body_weight==0||body_height==0){
                    Toast.makeText(getActivity(),"有选项没有选择",Toast.LENGTH_SHORT).show();
                }else{
                    body_apps.setBody_height_result(String.valueOf(body_height));
                    body_apps.setBody_weight_result(String.valueOf(body_weight));
                    body_apps.setFlag(4);
                    body_apps.setIntentflag(0);
                    double  BMI;
                    BMI = (body_weight) / ((body_height/100.0) * (body_height/100.0));
                    int bodyscore;
                    if (BMI > 14.0 && BMI < 28.0){
                        bodyscore = (int)(Math.abs (14.2857 * (21.0-BMI)));
                    }
                    else
                        bodyscore = 0;
                    body_apps.setBody_result(String.valueOf(bodyscore));
                    getActivity().finish();
                    Intent intent  =new Intent(getActivity(), TestAssess.class);
                    startActivity(intent);
                }
            }
        });

        return view;
    }

    public void SetNumberpicker(int id) {
        String name = null;
        factory = LayoutInflater.from(getActivity());
        if (id == R.id.bt_body_height) {
            name = "设置身高";
            flag = 1;
        } else if (id == R.id.bt_body_weight) {
            name = "设置体重";
            flag = 2;
        }
        initNumberpicker();
        Dialog dialog = new AlertDialog.Builder(getActivity())

                .setTitle(name)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int a) {
                        if(flag==1&&swich){
                            numSaver=170;
                        }else if(flag==2&&swich){
                            numSaver=50;
                        }
                        if (flag == 1) {
                            body_height = numSaver;
                            bt_height.setTextColor(Color.parseColor("#ff0000"));
                            bt_height.setText(String.valueOf(body_height));
                        } else if (flag == 2) {
                            body_weight = numSaver;
                            bt_weight.setTextColor(Color.parseColor("#ff0000"));
                            bt_weight.setText(String.valueOf(body_weight));
                        }
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).setView(myView).create();
        dialog.show();
    }

    public void initNumberpicker() {
        myView = factory.inflate(R.layout.activity_numberpicker, null);
         numberpicker = (NumberPicker) myView.findViewById(R.id.test_numberpicker);
        swich=true;
        if (flag==1){
            numberpicker.setMinValue(120);
            numberpicker.setMaxValue(210);
            numberpicker.setWrapSelectorWheel(false);
        }else if(flag==2){
            numberpicker.setMinValue(30);
            numberpicker.setMaxValue(120);
            numberpicker.setWrapSelectorWheel(false);
        }
        numberpicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                swich=false;
                numSaver = newVal;
            }
        });


    }
}
